#define _CRT_SECURE_NO_WARNINGS 1
#include "string1.h"
namespace zkf
{
	const size_t string::npos = -1;
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[]_str;
			_str = tmp;
			_capacity = n;
		}
	}
	void string::push_back(char s)
	{
		if (_capacity == _size)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		_str[_size++] = s;
		_str[_size] = '\0';
	}
	string& string::operator+=(char s)
	{
		push_back(s);
		return *this;
	}
	void string::append(const char* s)
	{
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}
		strcpy(_str + _size, s);
		_size += len;
	}
	string& string::operator+=(const char* s)
	{
		append(s);
		return *this;
	}
	void string::insert(size_t pos, char s)
	{
		assert(pos < _size);
		if (_capacity == _size)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		size_t as = _size + 1;
		while (as > pos)
		{
			_str[as] = _str[as - 1];
			as--;
		}
		_str[pos] = s;
		++_size;
	}
	void string::insert(size_t pos, const char* s)
	{
		size_t len=strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}
		size_t as = _size + len;
		while (as >= pos + len)
		{
			_str[as] = _str[as - len];
			as--;
		}
		for (size_t i = 0; i < len; i++)
		{
			_str[pos+i] = s[i];
		}
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			for (size_t i = pos + len; i <= _size; i++)
			{
				_str[i-len] = _str[i];
			}
			_size -= len;
		}
	}
	size_t string::find(char s, size_t pos)
	{
		assert(pos < _size);
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == s)
			{
				return i;
			}
		}
		return npos;

	}
	size_t string::find(const char* s, size_t pos)
	{
		assert(pos < _size);
		const char* sps = strstr(_str + pos, s);
		if (sps == nullptr)
		{
			return npos;
		}
		else
		{
			return sps - _str;
		}
	}
	string string::substr(size_t pos, size_t len)
	{
		assert(pos<_size);
		if (len > _size - pos)
		{
			len = _size - pos;
		}
		string sub;
		sub.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			sub+= _str[pos + i];
		}
		return sub;
	}
	bool operator<(const string& s1, const string& s2)
	{
		return strcmp(s1.tou(), s2.tou()) < 0;
	}
	bool operator<=(const string& s1, const string& s2)
	{
		return s1 < s2 || s1 == s2;
	}
	bool operator>(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}
	bool operator>=(const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}
	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.tou(), s2.tou()) == 0;
	}
	bool operator!=(const string& s1, const string& s2)
	{
		return s1 == s2;
	}
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		const size_t N = 256;
		char buff[N];
		char ch = in.get();
		size_t i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}
	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto e : s)
		{
			out << e;
		}
		return out;
	}
}
